package com.sammier.lesson04;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class URLDown {
    public static void main(String[] args) throws IOException {

        //1、下载地址
        URL url = new URL("https://video.amtbhk.net/mp4/12/12-047/12-047-0079.mp4");

        //2、连接到资源
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();

        InputStream inputStream = urlConnection.getInputStream();

        FileOutputStream fos = new FileOutputStream("F:\\妈妈的\\修华严奥旨\\妄盡還源觀 二講-第79集.mp4");
        byte[] buffer = new byte[65536];
        int length;

        long startTime = System.currentTimeMillis();  //⏲开始

        while ((length = inputStream.read(buffer)) != -1) {
            fos.write(buffer, 0,length);
        }

        long endTime = System.currentTimeMillis();  //⏲结束
        long duration = (endTime - startTime) / 1000; //转化为秒
        System.out.println("下载完成，总共用时: " + duration + " 秒");
        System.out.println(fos+"完成");
        fos.close();
        inputStream.close();
        urlConnection.disconnect();
    }
}
